package com.million.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.commonutils.JwtUtils;
import com.million.commonutils.R;
import com.million.eduservice.client.OrdersClient;
import com.million.eduservice.entity.EduCourse;
import com.million.commonutils.orderVo.CourseOrderVo;
import com.million.eduservice.entity.frontvo.course.CourseQueryVo;
import com.million.eduservice.entity.frontvo.course.CourseWebVo;
import com.million.eduservice.entity.vo.chapter.ChapterVo;
import com.million.eduservice.service.EduChapterService;
import com.million.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: studyboy
 * @Date: 2022/4/6  14:27
 */

@RestController
@RequestMapping("/eduservice/courseFront")
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrdersClient ordersClient;

    //分页查询课程信息
    @PostMapping("/getCourseList/{page}/{limit}")
    public R getCourseList (@ApiParam(name = "page", value = "当前页码", required = true)
                            @PathVariable("page") Long page,
                            @ApiParam(name = "limit", value = "每页记录数", required = true)
                            @PathVariable("limit") Long limit,
                            @RequestBody(required = false) CourseQueryVo courseQueryVo) {
        Page<EduCourse> coursePage = new Page<>(page, limit);
        Map<String, Object> map = eduCourseService.getCourseList(coursePage, courseQueryVo);
        return R.ok().data(map);
    }

    //根据课程id 获取课程详情
    @GetMapping("/getCourseInfoVoById/{courseId}")
    public R getFrontCourseInfo (@PathVariable("courseId") String courseId, HttpServletRequest request) {

        //根据d 获取课程信息，编写sql语句
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);

        //根据课程id 获取章节和小节
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoByCourseId(courseId);
        //memberId 为空，那么isBuy肯定为空，因为在selectOrderStatus方法中加了判断条件
        if(StringUtils.isEmpty(JwtUtils.getMemberIdByJwtToken(request))){
            return R.error().code(28004).message("请先登录");
        }
        //TODO 有个小bug  就是需要先登录 才能查看课程详情，没登录查看课程 详情前端会报错 已解决
        //根据课程id 和用户id 判断课程是否被支付
        boolean isBuy = ordersClient.selectOrderStatus(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList).data("isBuy", isBuy);
    }

    //根据课程id 获取课程名称和课程封面
    @GetMapping("getOrderCourseInfo/{courseId}")
    public CourseOrderVo getOrderCourseInfo (@PathVariable("courseId") String courseId) {
        CourseWebVo baseCourseInfo = eduCourseService.getBaseCourseInfo(courseId);
        CourseOrderVo courseOrderVo = new CourseOrderVo();
        BeanUtils.copyProperties(baseCourseInfo, courseOrderVo);
        return courseOrderVo;
    }

    //根据课程名查询课程id，通过课程id 找到课程详情
    @GetMapping("queryCourseByName/{courseName}")
    public R queryCourseByName(@PathVariable("courseName") String courseName){
       String courseId =  eduCourseService.queryCourseByName(courseName);
       return R.ok().data("courseId",courseId);
    }
}
